package com.example.practice.adapter;

import java.util.List;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.practice.R;
import com.example.practice.bean.SensorBean;

public class SensorGridAdapter extends BaseAdapter{
	private List<SensorBean> sensorBeans;
	public SensorGridAdapter( List<SensorBean> sensorBeans) {
		this.sensorBeans = sensorBeans;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return sensorBeans.size();
	}

	@Override
	public SensorBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return sensorBeans.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if(arg1 == null){
			viewHolder = new ViewHolder();
			arg1 = LayoutInflater.from(arg2.getContext()).inflate(R.layout.sensor_grid_item, null);
			viewHolder.name = (TextView) arg1.findViewById(R.id.tv_name);
			viewHolder.status = (TextView) arg1.findViewById(R.id.tv_status);
			viewHolder.settings = (TextView) arg1.findViewById(R.id.tv_settings);
			viewHolder.value = (TextView) arg1.findViewById(R.id.tv_value);
			viewHolder.bg = arg1.findViewById(R.id.bg);
			arg1.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) arg1.getTag();
		}
		SensorBean sensorBean =  sensorBeans.get(position);
		viewHolder.value.setText(sensorBean.getValue() + "");
		viewHolder.name.setText(sensorBean.getName());
		viewHolder.settings.setText("设置：" + sensorBean.getMinValue() +"-" + sensorBean.getMaxValue() );
		if(sensorBean.getMaxValue() <= sensorBean.getValue() || sensorBean.getMinValue() >= sensorBean.getValue()){
			viewHolder.bg.setBackgroundColor(Color.RED);
			viewHolder.status.setText("预警");
		}else{
			viewHolder.bg.setBackgroundColor(Color.GREEN);
			viewHolder.status.setText("正常");
		}
		
		return arg1;
	}

	private class ViewHolder{
		private TextView name;
		private TextView status;
		private TextView settings;
		private TextView value;
		private View bg;
	}
}
